import babelpolyfill from 'babel-polyfill'
import Vue from 'vue' //
import App from './App'
import ElementUI from 'element-ui' //
import 'element-ui/lib/theme-chalk/index.css' //
import './assets/icon/iconfont.css';
import VueRouter from 'vue-router' //
import store from './vuex/store'
import Vuex from 'vuex'
import routes from './routes'
//打包时候注意把MOCK删除
//import Mock from './mock'
//Mock.bootstrap();
import 'font-awesome/css/font-awesome.min.css'
import i18n from './i18n';
import jwt_decode from 'jwt-decode'


Vue.config.productionTip = false

Vue.use(ElementUI) //
Vue.use(VueRouter) //
Vue.use(Vuex)


const router = new VueRouter({
    routes
})

router.beforeEach((to, from, next) => {
    if (to.path == '/login') {
        next();
    }
    //如果本地TOKEN为空，则退到用户登录页面
    if ((localStorage.getItem("shopToken") == null || localStorage.getItem("shopToken").length == 0) && to.path != '/login') {
        localStorage.removeItem("shopToken");
        //sessionStorage.removeItem('user');
        next({ path: '/login' });
        return;
    }else{
        //TODO:判断是否解析异常
        var shopToken = "";
        try {
            shopToken = jwt_decode(localStorage.getItem("shopToken"));
            if(shopToken.shopid==null || shopToken.shopid.length<5){
                alert("恶意用户，您的信息已经被采集，勿要非法攻击网站！");
                localStorage.removeItem("shopToken");
                next({ path: '/login' });
                return;
            }
        }
        catch(err) {
            console.log("err:"+err.message);
            next({ path: '/login' });
            return;
        }        
        if (sessionStorage.getItem("userToken")==null && to.path != '/home') {
            sessionStorage.removeItem('userToken');
            next({ path: '/home' });
            return;
        } else {
            next()
        }
    }
    //NProgress.start();
})

//router.afterEach(transition => {
//NProgress.done();
//});



new Vue({
    //el: '#app',
    //template: '<App/>',
    router,
	i18n,
    store,
    //components: { App }
    render: h => h(App)
}).$mount('#app')